﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OCR
{
    public partial class RegionView : Form
    {
        private CardType cardType;

        public CardType CardType
        {
            get { return cardType; }
            set { cardType = value; }
        }
        private Bitmap bitmap;
        public RegionView(CardType cardType, Bitmap bitmap)
        {
            InitializeComponent();
            this.cardType = cardType;
            this.bitmap = bitmap;

        }

        private void RegionView_Load(object sender, EventArgs e)
        {
            ShowImage(this.cardType, this.bitmap);
        }

        public void ShowImage(CardType cardType, Bitmap bitmap)
        {
            this.cardType = cardType;
            this.bitmap = bitmap;
            this.ckbNoCharNoiseClear.Checked = cardType.NoRecognition.CharRecognition.NoiseClear;
            this.ckbNoBarCodeNoiseClear.Checked = cardType.NoRecognition.BarCodeRecognition.NoiseClear;
            this.ckbPwCharNoiseClear.Checked = cardType.PwRecognition.CharRecognition.NoiseClear;
            this.ckbPwBarCodeNoiseClear.Checked = cardType.PwRecognition.BarCodeRecognition.NoiseClear;

            if (this.cardType.NoRecognition.CharRecognition.Region != null && this.cardType.NoRecognition.CharRecognition.Region.Width != 0)
            {
                this.pbNoChar.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.NoRecognition.CharRecognition.Region, BinarizationMethods.Otsu, this.ckbNoCharNoiseClear.Checked);
            }
            else
            {
                this.pbNoChar.Image = null;
            }
            if (this.cardType.NoRecognition.BarCodeRecognition.Region != null && this.cardType.NoRecognition.BarCodeRecognition.Region.Width != 0)
            {
                this.pbNoBarCode.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.NoRecognition.BarCodeRecognition.Region, BinarizationMethods.Otsu, this.ckbNoBarCodeNoiseClear.Checked);
            }
            else
            {
                this.pbNoBarCode.Image = null;
            }
            if (this.cardType.PwRecognition.CharRecognition.Region != null && this.cardType.PwRecognition.CharRecognition.Region.Width != 0)
            {
                this.pbPwChar.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.PwRecognition.CharRecognition.Region, BinarizationMethods.Otsu, this.ckbPwCharNoiseClear.Checked);
            }
            else
            {
                this.pbPwChar.Image = null;
            }
            if (this.cardType.PwRecognition.BarCodeRecognition.Region != null && this.cardType.PwRecognition.BarCodeRecognition.Region.Width != 0)
            {
                this.pbPwBarCode.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.PwRecognition.BarCodeRecognition.Region, BinarizationMethods.Otsu, this.ckbPwBarCodeNoiseClear.Checked);
            }
            else
            {
                this.pbPwBarCode.Image = null;
            }
        }

        private void RegionView_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Visible = false;
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }
        private void ckbNoCharNoiseClear_CheckedChanged(object sender, EventArgs e)
        {
            this.cardType.NoRecognition.CharRecognition.NoiseClear = this.ckbNoCharNoiseClear.Checked;
            if (this.cardType.NoRecognition.CharRecognition.Region.Width != 0)
            {
                this.pbNoChar.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.NoRecognition.CharRecognition.Region, BinarizationMethods.Otsu, this.ckbNoCharNoiseClear.Checked);
            }
        }

        private void ckbNoBarCodeNoiseClear_CheckedChanged(object sender, EventArgs e)
        {
            this.cardType.NoRecognition.BarCodeRecognition.NoiseClear = this.ckbNoBarCodeNoiseClear.Checked;
            if (this.cardType.NoRecognition.BarCodeRecognition.Region.Width != 0)
            {
                this.pbNoBarCode.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.NoRecognition.BarCodeRecognition.Region, BinarizationMethods.Otsu, this.ckbNoBarCodeNoiseClear.Checked);
            }
        }
        private void ckbPwCharNoiseClear_CheckedChanged(object sender, EventArgs e)
        {
            this.cardType.PwRecognition.CharRecognition.NoiseClear = this.ckbPwCharNoiseClear.Checked;
            if (this.cardType.PwRecognition.CharRecognition.Region.Width != 0)
            {
                this.pbPwChar.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.PwRecognition.CharRecognition.Region, BinarizationMethods.Otsu, this.ckbPwCharNoiseClear.Checked);
            }
        }
        private void ckbPwBarCodeNoiseClear_CheckedChanged(object sender, EventArgs e)
        {
            this.cardType.PwRecognition.BarCodeRecognition.NoiseClear = this.ckbPwBarCodeNoiseClear.Checked;
            if (this.cardType.PwRecognition.BarCodeRecognition.Region.Width != 0)
            {
                this.pbPwBarCode.Image = BitmapHelper.CutProcessImage(bitmap, this.cardType.PwRecognition.BarCodeRecognition.Region, BinarizationMethods.Otsu, this.ckbPwBarCodeNoiseClear.Checked);
            }
        }
        Bitmap pengzhangBitmap;
        private void ckbNoCharBold_CheckedChanged(object sender, EventArgs e)
        {
            
            Bitmap tempBitmap=BitmapHelper.CutProcessImage(bitmap, this.cardType.NoRecognition.CharRecognition.Region, BinarizationMethods.Otsu,false);
             pengzhangBitmap = tempBitmap.Clone(new Rectangle(0, 0, tempBitmap.Width, tempBitmap.Height), PixelFormat.Format24bppRgb);
            this.pbNoChar.Image = BitmapHelper.GetPengZhangImage(pengzhangBitmap);
        }
    }
}
